#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "symbol.h"

int Hash(char *str){
	unsigned hash = 0;
	/* loop over the entire string */
	while(*str != 0){
		/* add the ascii value of the sign to the sum*/
		hash = hash + (int)*str;
		/* move pointer to next and check if its 0 */
		str++;
		if(*str != 0){
			/* if not then shift */
			hash=hash<<1;
		}
	}
	return (hash%HashSize);
}
SymbolTable *initSymbolTable(){
	/* a table is created. */
	SymbolTable *tmp = NEW(SymbolTable);
	for(int i=0;i<HashSize;i++){
		tmp->table[i] = NULL;
	}
	tmp->next = NULL;
	printf("SymbolTable initialized: |%d|\n", tmp);
	return tmp;
}
SymbolTable *scopeSymbolTable(SymbolTable *t){
	/* a table is created using the method above. */
	SymbolTable *new = initSymbolTable();
	/* the pointer in next is set to point to the table given as input */
	new->next = t;
	printf("scopeTable: parent: |%d|\n", new->next);
	return new;
}
SYMBOL *putSymbol(SymbolTable *t, char *name, int value){
	/* find the bucket in the table where the SYMBOL should be stored */
	int bucket = Hash(name);
	if(t->table[bucket] == NULL){ // edited
		/* if the bucket is empty then use the bucket with a new SYMBOL */
		SYMBOL *tmp = NEW(SYMBOL);
		tmp->name = name;  // HER SKAL LAVES  NOGET ANDET!!!!!!!!!!!
		tmp->value = value;
		return tmp;
	} else {
		SYMBOL *tmp = t->table[bucket];
		if(strcmp(tmp->name, name)==0){
			printf("putSymbol: (%s, %d) trying to insert duplicate... Not allowed \n",name,value);
			return NULL;
		}
		/* else find the next empty in the chain of symbols */
		while(tmp->next != NULL){
			/* if the name exists then return null */
			tmp = tmp->next;
			if(strcmp(tmp->name, name)==0){
				printf("putSymbol: (%s, %d) trying to insert duplicate... Not allowed \n",name,value);
				return NULL;
			}

		}
		/* if empty symbol is found then use it and set the
		   next-pointer of the previous symbol to this symbol */
		SYMBOL *new = NEW(SYMBOL);
		new->name = name;
		new->value = value;
		tmp->next = new;
		tmp=new;
		return tmp;
	}
}
SYMBOL *getSymbol(SymbolTable *t, char *name){
	/* find where the name's bucket is*/
    	int index = Hash(name);
	if(t->table[index] != NULL){
		/* if a symbol exists */
		SYMBOL *symbol= t->table[index];
		while(strcmp(symbol->name, name)!=0 && symbol->next!=NULL){
			symbol = symbol->next;
		}
		if(strcmp(symbol->name, name)==0){
			return symbol;
		}else{
			if(t->next != NULL){
				return getSymbol(t->next, name);
			}else{
				return NULL;
			}
		}
	} else {
		/* if there is nothing in the bucket */
		if(t->next != NULL){
			return getSymbol(t->next, name);
		}else{
			return NULL;
		}
	}
}
void dumpSymbolTable(SymbolTable *t){
	SYMBOL *current;
	int tmp = 0;
	printf("dumpSymbolTable: %d\n", t);
	do{
		/* if this is not the first runthrough then go to next table */
		if(tmp>0){
			t=t->next;
		}
		printf("LOL");
		/* run through the entire table */
		for(int i=0;i<HashSize; i++){
			if(t->table[i]!=NULL){
				printf("LOL2");
				current = t->table[i];
				printf("table: %d pair (%s, %d) - %d \n",t ,current->name, current->value, i);
				while(current->next!= NULL){
					current= current->next;
					printf("table: %d pair (%s, %d) - %d \n",t ,current->name, current->value, i);
				}
			}
		}
		tmp++;
	}while(t->next!=NULL);
}
